home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9059 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: cs.tu-berlin.de!news
  2. From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overloaded Virtual functions
  5. Date: 28 Feb 1996 02:42:29 GMT
  6. Organization: Technical University of Berlin, Germany
  7. Message-ID: <4h0fel$pru@news.cs.tu-berlin.de>
  8. NNTP-Posting-Host: 130.149.17.236
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=iso-8859-1
  11. Content-Transfer-Encoding: 8bit
  12.  
  13. Kevin Ingalls <ingkl900@ccmail.ca.boeing.com> writes:
  14. > sonia_mangat@bdsi.com (Sonia Mangat) wrote:
  15. > >Hi everybody,
  16. > >I have a question on overloaded virtual functions. I have included a
  17. > >program that has a parent class with overloaded virtual functions.
  18. > >I have also attached the compilation error I get.
  19. > >
  20. > >The ways I can think of correcting this problem are
  21. > > --> Declare the other prototype in the class B and make it call it's
  22. > >     parent's function.
  23. > > --> Give different names for the virtual function.
  24. > >
  25. > > I don't like both these options. Is there any other way to solve this 
  26. > >problem?
  27. > >
  28. > >Please respond to : vidhya@bdsi.com
  29. > >
  30. > >Thanks in advance
  31. > >vidhya
  32. > I think that you have been misled about virtual functions. In order to make
  33. > use of virtual functions, you need pointers to base classes. The virtual function
  34. > mechanism does not work if you use objects directly. I have reworked your code
  35. > example to make use of virtual functions. I hope that this helps. Let me know
  36. > if you need clarification.
  37.  
  38. In this case, the problem is not the virtual function mechanism. Actually, it does
  39. work: if you declare B b and then call b.print(0)   B::print is called and this is
  40. what virtual functions are all about ( the mechanism isn't used in this particular
  41. case, but the result is the same ). The point is that when you declare a base class
  42. with a set of overloaded virtual functions and reimplement one of these functions
  43. in a derived class ALL base class functions with the same name are hidden by the
  44. reimplemented function. That means you need a qualifier: simply replace b.print(p)
  45. by b.A::print(p), i.e. tell the compiler to call A's function. 
  46. I think this is a serious weakness of the language since the unique identifier of 
  47. a function is ( or should be ) its name and the types of its arguments. Thus, the
  48. functions print(int) and print(const& T) shouldn't be related at all. You won't 
  49. expect a function named print to hide a function named read, would you?
  50.  
  51. Bye
  52.  
  53. Roman
  54.  
  55.  
  56.